
use super::model;
use axum::extract;


/// "/:id"
/// get
pub async fn get_user_by_id(extract::Path(id): extract::Path<u32>) -> axum::Json<Option<model::UserDTO>>
{
    let rb = crate::db::get_rbatis();
    let result = model::User::select_by_id(rb, &id.to_string()).await;
    return if let Result::Ok(option) = result {
        if option.is_none() { return axum::Json(Option::None); }
        axum::Json(Option::Some(model::UserDTO::new(option.unwrap())))
    } else { eprintln!("database select error\n{}", result.unwrap_err());axum::Json(Option::None) }
}


/// "/register"
/// post
pub async fn register(axum::Json(user): axum::Json<model::User>) -> axum::response::Html<String>
{
    let rb = crate::db::get_rbatis();
    let success = axum::response::Html(rust_i18n::t!("user.register.success"));
    let failed = axum::response::Html(rust_i18n::t!("user.register.failed.default"));
    let failed_exists = axum::response::Html(rust_i18n::t!("user.register.failed.exists"));

    let result = model::User::select_by_column(rb, "username", user.get_username()).await;
    if let Result::Ok(list) = result {
        if list.len() != 0 { return failed_exists; }
    } else { eprintln!("database select error\n{}", result.unwrap_err());return failed; }

    let result = model::User::insert(rb, &user).await;
    if let Result::Ok(row) = result {
        if row.rows_affected == 0 { return failed; }
    } else { eprintln!("database insert error\n{}", result.unwrap_err()); }
    success
}


/// "/login"
/// post
pub async fn login
    (
        session: axum_session::Session<axum_session::SessionPgPool>,
        axum::Json(user): axum::Json<model::User>
    ) -> axum::response::Html<String>
{
    let rb = crate::db::get_rbatis();
    let success = axum::response::Html(rust_i18n::t!("user.login.success"));
    let failed = axum::response::Html(rust_i18n::t!("user.login.failed.default"));
    let failed_not_exists = axum::response::Html(rust_i18n::t!("user.login.failed.not_exists"));
    let failed_password_wrong = axum::response::Html(rust_i18n::t!("user.login.failed.password_wrong"));

    let result = model::User::select_by_column(rb, "username", user.get_username()).await;
    let mut 要登的号 = if let Ok(list) = result {
        if list.len() == 0 { return failed_not_exists; }
        list[0].clone()
    } else {
        eprintln!("database select error\n{}", result.unwrap_err());
        return failed;
    };

    if user.get_password() != 要登的号.get_password() {
        return failed_password_wrong;
    }

    let mut tx = if let Ok(tx) = rb.acquire_begin().await {
        tx
    } else { return failed; };
    要登的号.set_last_login_time(rbatis::rbdc::datetime::DateTime::now());
    let result = model::User::update_by_column(rb, &要登的号, "id").await;
    if let Err(e) = result {
        eprintln!("database update error\n{}", e);
        tx.commit().await.unwrap();
        tx.rollback().await.unwrap();
        return failed;
    }
    tx.commit().await.unwrap();

    session.set_store(true);
    session.set("user", 要登的号);
    success
}



